package com.afu.demo;

import cn.hutool.core.date.StopWatch;
import javafx.scene.paint.Stop;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * ClassName: Demo3
 * Package: com.afu
 * Description:
 *   arrayList和linkList随机读取耗时对比
 *
 *
 * @Author zxf
 * @Create 2024/2/6 14:51
 * @Version 1.0
 */
public class Demo3 {
    public static void main(String[] args) {
        List<Integer> intArrayList = new ArrayList<>();
        List<Integer> intLinkedList = new LinkedList<>();


        for (int i = 0; i < 100000; i++) {
            intArrayList.add(i);
            intLinkedList.add(i);
        }


        StopWatch stopWatch = new StopWatch();

        stopWatch.start("linkList");
        arrayRead(intLinkedList);
        stopWatch.stop();

        stopWatch.start("arrayList");
        arrayRead(intArrayList);
        stopWatch.stop();

        System.out.println(stopWatch.prettyPrint());
    }

    public static void arrayRead(List<Integer>  list){
        Random random = new Random();
        for (Integer number : list) {
            int randomIndex = random.nextInt(list.size());
            Integer element = list.get(randomIndex);
        }
    }
}
